﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessHandler;
using EasyCode.Utility;

public partial class Page_AddEditAdvertisement : System.Web.UI.Page
{
    private AdvertisementItem item;
    private AdvertisementItemController advertisementItemController;
    private AdvertisementCategoryController advertisementCategoryController;
    private AdvertisementPositionController advertisementPositionController;
    private string imgFolder = System.Configuration.ConfigurationManager.AppSettings["ADV_IMG_FOLDER"];
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.GetCategory();
            this.GetPosition();
            this.LoadDetail();
        }
    }
    private void GetCategory()
    {
        advertisementCategoryController = AdvertisementCategoryController.Instance;
        this.DropDownList_Category.DataSource = advertisementCategoryController.GetCategoryList(true);
        this.DropDownList_Category.DataTextField = "AdvCatName";
        this.DropDownList_Category.DataValueField = "AdvCatID";
        this.DropDownList_Category.DataBind();
        this.DropDownList_Category.Items.Insert(0, new ListItem("--Select category--", "0"));
    }
    private void GetPosition()
    {
        advertisementPositionController = AdvertisementPositionController.Instance;
        this.DropDownList_Position.DataSource = advertisementPositionController.GetPositionList(true);
        this.DropDownList_Position.DataValueField = "Id";
        this.DropDownList_Position.DataTextField = "Name";
        this.DropDownList_Position.DataBind();
        this.DropDownList_Position.Items.Insert(0, new ListItem("--Select display position--", "0"));

    }
    public string GetPageTitle()
    {
        return "Add New Advertisement";
    }
    protected void Button_Save_Click(object sender, EventArgs e)
    {
        AdvertisementDataContext datacontext = new AdvertisementDataContext();
        DateTime startDate;
        DateTime endDate;
        DateTime now = Protector.DateTime(DateTime.Now.ToShortDateString());
        string linkFile = string.Empty;
        string error = string.Empty;
        if (this.Button_Save.Text.Trim().ToUpper() == "SAVE")
        {
            if (!CheckRoles.CanInsert())
            {
                Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
            }
            this.item = new AdvertisementItem();
        }
        else
        {
            if (!CheckRoles.CanEdit())
            {
                Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
            }
            this.item = (from o in datacontext.AdvertisementItems
                         where o.Id == Protector.Int(Session["currenadvid"], 0)
                         select o).SingleOrDefault();
            if (this.item == null)
            {
                Response.Redirect("");
            }
        }
        this.item.Name = HttpUtility.HtmlEncode(this.TextBox_AdvTitle.Text.Trim());
        this.item.Detail = HttpUtility.HtmlEncode(this.TextBox_Detail.Text.Trim());
        this.item.Link = HttpUtility.HtmlEncode(this.TextBox_Link.Text.Trim());
        if (!DateTime.TryParse(this.TextBox_StartDate.Text, out startDate))
        {
            this.Message_Alert.SetError(Resources.Advertisement.ConfirmInputStartDate);
            return;
        }
        if (!DateTime.TryParse(this.TextBox_EndDate.Text, out endDate))
        {
            this.Message_Alert.SetError(Resources.Advertisement.ConfirmInputEndDate);
            return;
        }
        if (DateTime.Compare(startDate, now) < 0)
        {
            this.Message_Alert.SetError(Resources.Advertisement.InvalidStartDate);
            return;
        }
        if (DateTime.Compare(endDate, startDate) < 0)
        {
            this.Message_Alert.SetError(Resources.Advertisement.ConflickDate);
            return;
        }
        if (this.DropDownList_Category.SelectedIndex < 1)
        {
            this.Message_Alert.SetError(Resources.Advertisement.ConfirmSelectCategory);
            return;
        }
        if (this.DropDownList_Position.SelectedIndex < 1)
        {
            this.Message_Alert.SetError(Resources.Advertisement.ConfirmSelectCategoryPosition);
            return;
        }
        if (this.Button_Save.Text.Trim().ToUpper() == "SAVE")
        {
            if (this.FileUpload_Image.HasFile)
            {
                if (Commons.Upload(this.FileUpload_Image, imgFolder, new string[] { ".png", ".jpg", ".jpeg", ".flv" }, ref linkFile, ref error))
                {
                }
                else
                {
                    this.Message_Alert.SetError(Resources.News.UploadImageError + " " + error);
                    return;
                }
            }
            else
            {
                this.Message_Alert.SetError(Resources.Advertisement.ConfirmSelectAdvertisementFile + " " + error);
                return;

            }

        }
        else
        {
            if (this.FileUpload_Image.HasFile)
            {
                if (Commons.Upload(this.FileUpload_Image, imgFolder, new string[] { ".png", ".jpg", ".jpeg" }, ref linkFile, ref error))
                {
                }
            }
        }
        if (!string.IsNullOrEmpty(linkFile))
        {
            this.item.FileLink1 = linkFile;
        }
        this.item.AdvCategoryID = Protector.Int(this.DropDownList_Category.SelectedValue);
        this.item.PositionID = Protector.Int(this.DropDownList_Position.SelectedValue);
        this.item.StartTime = startDate;
        this.item.EndTime = endDate;
        this.item.Created_At = now;
        if (this.Button_Save.Text.Trim().ToUpper() == "SAVE")
        {
            try
            {
                advertisementItemController = AdvertisementItemController.Instance;
                item.flag = true;
                if (advertisementItemController.Insert(item) > 0)
                {
                    this.Message_Alert.SetSuccess(Resources.Advertisement.SaveSuccess);
                    this.Reset();
                }
                else
                {
                    this.Message_Alert.SetError(Resources.Advertisement.SaveFail);
                }
            }
            catch (Exception ex)
            {
                this.Message_Alert.SetError(Resources.Advertisement.SaveFail + " Error: " + ex.Message);
            }
        }
        else if (this.Button_Save.Text.Trim().ToUpper() == "UPDATE")
        {
            try
            {
                datacontext.SubmitChanges();
                this.Message_Alert.SetSuccess(Resources.Advertisement.AdvertisementUpdateSuccess);
            }
            catch (Exception ex)
            {
                this.Message_Alert.SetError(Resources.Advertisement.AdvertisementUpdateFail + " Error: " + ex.Message);
            }
        }
    }
    private void Reset()
    {
        this.TextBox_AdvTitle.Text = string.Empty;
        this.DropDownList_Category.SelectedIndex = 0;
        this.DropDownList_Position.SelectedIndex = 0;
        this.TextBox_Link.Text = string.Empty;
        this.TextBox_StartDate.Text = string.Empty;
        this.TextBox_EndDate.Text = string.Empty;
        this.TextBox_Detail.Text = string.Empty;
    }
    private void LoadDetail()
    {
        int id = Protector.Int(Request.QueryString["id"], 0);
        if (id > 0)
        {
            this.advertisementItemController = AdvertisementItemController.Instance;
            this.item = this.advertisementItemController.FindItemByID(id);
            if (this.item == null)
            {
                Response.Redirect("");
            }
            this.TextBox_AdvTitle.Text = HttpUtility.HtmlDecode(item.Name);
            this.DropDownList_Category.SelectedValue = item.AdvCategoryID.ToString();
            this.DropDownList_Position.SelectedValue = item.PositionID.ToString();
            this.TextBox_Link.Text = HttpUtility.HtmlDecode(item.Link);
            this.TextBox_StartDate.Text = item.StartTime.Value.ToShortDateString();
            this.TextBox_EndDate.Text = item.EndTime.Value.ToShortDateString();
            this.TextBox_Detail.Text = HttpUtility.HtmlDecode(item.Detail);
            this.Button_Save.Text = "Update";
            Session["currenadvid"] = id;
        }
    }
}